package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.entities.Consumer;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/9 21:16
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    private ConsumerMapper consumerMapper;

    @Override
    public PageVO<ConsumerVO> findConsumerList(Integer pageNum, Integer pageSize, ConsumerVO consumerVO) {

        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        // 设置排序规则
        example.orderBy("sort").asc().orderBy("name").asc();

        if (!StringUtils.isEmpty(consumerVO.getName())) {
            criteria.andLike("name", "%" + consumerVO.getName() + "%");
        }

        if (!StringUtils.isEmpty(consumerVO.getAddress())) {
            criteria.andLike("address", "%" + consumerVO.getAddress() + "%");
        }

        if (!StringUtils.isEmpty(consumerVO.getContact())) {
            criteria.andLike("contact", "%" + consumerVO.getContact() + "%");
        }


        List<Consumer> consumers = consumerMapper.selectByExample(example);
        List<ConsumerVO> collect = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO1 = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO1);
            return consumerVO1;

        }).collect(Collectors.toList());

        PageInfo<Consumer> info = new PageInfo<>(consumers);
        return new PageVO<>(info.getTotal(), collect);
    }

    @Override
    public void addConsumer(ConsumerVO consumerVO) {
        // insertSelective 不插入为空的属性
        // insert 插入为空的属性
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setCreateTime(new Date());
        consumer.setModifiedTime(consumer.getCreateTime());
        consumerMapper.insertSelective(consumer);
    }

    @Override
    public ConsumerVO getConsumer(Long id) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(id);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer, consumerVO);
        return consumerVO;
    }

    @Override
    public void updateConsumer(Long id, ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKeySelective(consumer);
    }

    @Override
    public void deleteConsumer(Long id) {
        consumerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ConsumerVO> findAll() {
        List<Consumer> consumers = consumerMapper.selectAll();
        List<ConsumerVO> collect = consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

        return collect;

    }
}
